﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PlaceHR.Processor.Processor.DailyEvent.Steps;

namespace PlaceHR.Processor.Processor
{
    class DailyEventProcessor : ProcessorBase<DailyEventProcessorContext, IList<EmployeeAlert>>
    {
        #region Private Methods

        protected override DailyEventProcessorContext LoadCurrentContext(Employee employee, IEngine currentEngine)
        {
            if (employee == null || currentEngine == null)
            {
                throw new Exception("employee or currentEngine is null!");
            }

            DailyEventProcessorContext context = CurrentContext;

            if (context == null)
            {
                context = new DailyEventProcessorContext
                {
                    CurrentEmployee = employee,
                    CurrentEngine = currentEngine,
                    HRDBContext = currentEngine.HRDBContext
                };
            }

            this.LoadGlobalSettings(employee, currentEngine, context);

            return context;
        }

        protected override IList<EmployeeAlert> LoadCurrentEntity(Employee employee, IEngine currentEngine)
        {
            var alertList = CurrentContext.CurrentEngine.HRDBContext.EmployeeAlert.Where(
                s => s.EmployeeID == employee.EmployeeID 
                    && !s.IsResolved).ToList();

            //TODO: load Entity

            return alertList;
        }

        protected override void RegisterStepCalculators()
        {
            this.Register(new WorkingAgeStepCalculator());
            this.Register(new ContractExpiredStepCalculator());
            this.Register(new VisaExpiredStepCalculator());
            this.Register(new FinalizeDailyEventStepCalculator());
        }

        public override bool IsValidForProcess
        {
            get {
                return this.CurrentContext != null 
                    && this.CurrentEntity != null;
            }
        }

        private void LoadVacationBase(Employee employee, IEngine currentEngine, ref List<EmployeeAlert> vacationBaseList)
        {
            if (vacationBaseList == null)
            {
                vacationBaseList = new List<EmployeeAlert>();
            }
        }


        private void LoadGlobalSettings(Employee employee, IEngine currentEngine, DailyEventProcessorContext context)
        {
            GlobalSettings defaultGlobalSetting = currentEngine.HRDBContext.GlobalSettings.FirstOrDefault(p => p.OfficeCode == null);

            string officeCode = employee.OfficeDepartment_lnk.FirstOrDefault().OfficeCode;
            GlobalSettings currentGlobalSetting = currentEngine.HRDBContext.GlobalSettings.FirstOrDefault(p => p.OfficeCode != null && p.OfficeCode.Equals(officeCode, StringComparison.OrdinalIgnoreCase));

            if (defaultGlobalSetting == null || currentGlobalSetting == null)
            {
                throw new Exception("Global setting is invalid");
            }

            context.WorkingDaysPerMonth = currentGlobalSetting.WorkingDaysPerMonth.HasValue ? currentGlobalSetting.WorkingDaysPerMonth.Value : defaultGlobalSetting.WorkingDaysPerMonth.Value;
            context.WorkingHoursPerDay = currentGlobalSetting.WorkingHoursPerDay.HasValue ? currentGlobalSetting.WorkingHoursPerDay.Value : defaultGlobalSetting.WorkingHoursPerDay.Value;
            context.PersonalIncomeTaxbase = currentGlobalSetting.PersonalIncomeTaxBase.HasValue ? currentGlobalSetting.PersonalIncomeTaxBase.Value : defaultGlobalSetting.PersonalIncomeTaxBase.Value;
        }

        #endregion
    }
}
